#coding=utf-8
#Boa:FramePanel:PanelProductVariation

import wx

from product import CDataProduct, CDataCustomer
from MyListCtrl import MyListCtrlProSaleDetail, MyListCtrlProPurDetail

[wxID_PANELPRODUCTVARIATION, wxID_PANELPRODUCTVARIATIONBTNMANUALNUM, 
 wxID_PANELPRODUCTVARIATIONBTN_SAVE, wxID_PANELPRODUCTVARIATIONBUTTON_ENABLE, 
 wxID_PANELPRODUCTVARIATIONCHECKBOX_REGION, 
 wxID_PANELPRODUCTVARIATIONCHECKBOX_SHOWPEIHUO, 
 wxID_PANELPRODUCTVARIATIONSTATICTEXT1, wxID_PANELPRODUCTVARIATIONSTATICTEXT2, 
 wxID_PANELPRODUCTVARIATIONSTATICTEXT3, wxID_PANELPRODUCTVARIATIONSTATICTEXT4, 
 wxID_PANELPRODUCTVARIATIONSTATICTEXT5, wxID_PANELPRODUCTVARIATIONSTATICTEXT6, 
 wxID_PANELPRODUCTVARIATIONSTATICTEXT7, wxID_PANELPRODUCTVARIATIONSTATICTEXT8, 
 wxID_PANELPRODUCTVARIATIONSTATICTEXT9, 
 wxID_PANELPRODUCTVARIATIONTCCHEGUANSUO, 
 wxID_PANELPRODUCTVARIATIONTCCHEGUANSUOPOS, wxID_PANELPRODUCTVARIATIONTCJINBO, 
 wxID_PANELPRODUCTVARIATIONTCMANUALNUM, wxID_PANELPRODUCTVARIATIONTCMEMO, 
 wxID_PANELPRODUCTVARIATIONTCNAME, wxID_PANELPRODUCTVARIATIONTCONHAND, 
 wxID_PANELPRODUCTVARIATIONTCPOSITION, wxID_PANELPRODUCTVARIATIONTCSPEC, 
 wxID_PANELPRODUCTVARIATIONTCWUMING, wxID_PANELPRODUCTVARIATIONTCWUMINGPOS, 
 wxID_PANELPRODUCTVARIATIONTC_FREIGHT_CHARGES, 
 wxID_PANELPRODUCTVARIATIONTC_JINBOPOS, wxID_PANELPRODUCTVARIATIONTC_WEIGHT, 
 wxID_PANELPRODUCTVARIATIONTMANUALNUM, wxID_PANELPRODUCTVARIATIONTNAME, 
 wxID_PANELPRODUCTVARIATIONTONHAND, 
] = [wx.NewId() for _init_ctrls in range(32)]

class PanelProductVariation(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELPRODUCTVARIATION,
              name=u'PanelProductVariation', parent=prnt, pos=wx.Point(521,
              297), size=wx.Size(1113, 702), style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(1097, 664))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.tName = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONTNAME,
              label=u'\u5546\u54c1\uff1a', name=u'tName', parent=self,
              pos=wx.Point(16, 16), size=wx.Size(51, 16), style=0)

        self.tcName = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCNAME,
              name=u'tcName', parent=self, pos=wx.Point(72, 8),
              size=wx.Size(272, 26), style=0, value=u'')
        self.tcName.SetEditable(True)
        self.tcName.Enable(True)

        self.tOnhand = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONTONHAND,
              label=u'\u5e93\u5b58\uff1a', name=u'tOnhand', parent=self,
              pos=wx.Point(368, 16), size=wx.Size(51, 16), style=0)

        self.tcOnhand = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCONHAND,
              name=u'tcOnhand', parent=self, pos=wx.Point(424, 8),
              size=wx.Size(100, 26), style=0, value=u'')
        self.tcOnhand.SetEditable(False)
        self.tcOnhand.Enable(False)

        self.tcSpec = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCSPEC,
              name=u'tcSpec', parent=self, pos=wx.Point(72, 40),
              size=wx.Size(272, 26), style=0, value=u'')
        self.tcSpec.SetEditable(True)
        self.tcSpec.Enable(True)

        self.btnManualNum = wx.Button(id=wxID_PANELPRODUCTVARIATIONBTNMANUALNUM,
              label=u'\u4fee\u6539', name=u'btnManualNum', parent=self,
              pos=wx.Point(1032, 8), size=wx.Size(53, 26), style=0)
        self.btnManualNum.Bind(wx.EVT_BUTTON, self.OnBtnManualNumButton,
              id=wxID_PANELPRODUCTVARIATIONBTNMANUALNUM)

        self.tcManualNum = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCMANUALNUM,
              name=u'tcManualNum', parent=self, pos=wx.Point(928, 8),
              size=wx.Size(100, 26), style=0, value=u'')

        self.tManualNum = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONTMANUALNUM,
              label=u'\u624b\u5de5\u7f16\u53f7\uff1a', name=u'tManualNum',
              parent=self, pos=wx.Point(840, 16), size=wx.Size(85, 16),
              style=0)

        self.TCPosition = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCPOSITION,
              name=u'TCPosition', parent=self, pos=wx.Point(424, 40),
              size=wx.Size(100, 26), style=0, value=u'')
        self.TCPosition.Enable(False)
        self.TCPosition.Show(False)

        self.staticText1 = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONSTATICTEXT1,
              label=u'\u89c4\u683c\uff1a', name='staticText1', parent=self,
              pos=wx.Point(16, 48), size=wx.Size(51, 16), style=0)

        self.staticText2 = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONSTATICTEXT2,
              label=u'\u4ed3\u4f4d\uff1a', name='staticText2', parent=self,
              pos=wx.Point(368, 48), size=wx.Size(51, 16), style=0)
        self.staticText2.Show(False)

        self.btn_save = wx.Button(id=wxID_PANELPRODUCTVARIATIONBTN_SAVE,
              label=u'\u4fdd\u5b58\u4fee\u6539', name=u'btn_save', parent=self,
              pos=wx.Point(728, 40), size=wx.Size(93, 26), style=0)
        self.btn_save.Enable(False)
        self.btn_save.Bind(wx.EVT_BUTTON, self.OnBtn_saveButton,
              id=wxID_PANELPRODUCTVARIATIONBTN_SAVE)

        self.TCMemo = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCMEMO,
              name=u'TCMemo', parent=self, pos=wx.Point(600, 8),
              size=wx.Size(224, 26), style=0, value=u'')

        self.staticText3 = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONSTATICTEXT3,
              label=u'\u5907\u6ce8\uff1a', name='staticText3', parent=self,
              pos=wx.Point(544, 16), size=wx.Size(51, 16), style=0)

        self.button_enable = wx.Button(id=wxID_PANELPRODUCTVARIATIONBUTTON_ENABLE,
              label=u'\u5f00\u59cb\u7f16\u8f91', name=u'button_enable',
              parent=self, pos=wx.Point(600, 40), size=wx.Size(93, 26),
              style=0)
        self.button_enable.Bind(wx.EVT_BUTTON, self.OnButton_enableButton,
              id=wxID_PANELPRODUCTVARIATIONBUTTON_ENABLE)

        self.checkBox_region = wx.CheckBox(id=wxID_PANELPRODUCTVARIATIONCHECKBOX_REGION,
              label=u'\u663e\u793a\u672c\u5730\u533a\u5ba2\u6237',
              name=u'checkBox_region', parent=self, pos=wx.Point(928, 48),
              size=wx.Size(160, 16), style=0)
        self.checkBox_region.SetValue(False)
        self.checkBox_region.Bind(wx.EVT_CHECKBOX,
              self.OnCheckBox_regionCheckbox,
              id=wxID_PANELPRODUCTVARIATIONCHECKBOX_REGION)

        self.staticText4 = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONSTATICTEXT4,
              label=u'\u91cd\u91cf\uff1a', name='staticText4', parent=self,
              pos=wx.Point(16, 80), size=wx.Size(51, 16), style=0)

        self.tc_weight = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTC_WEIGHT,
              name=u'tc_weight', parent=self, pos=wx.Point(72, 72),
              size=wx.Size(72, 26), style=0, value=u'0')

        self.staticText5 = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONSTATICTEXT5,
              label=u'kg', name='staticText5', parent=self, pos=wx.Point(152,
              80), size=wx.Size(18, 16), style=0)

        self.staticText6 = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONSTATICTEXT6,
              label=u'\u8fd0\u8d39\uff1a', name='staticText6', parent=self,
              pos=wx.Point(208, 80), size=wx.Size(51, 16), style=0)

        self.tc_freight_charges = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTC_FREIGHT_CHARGES,
              name=u'tc_freight_charges', parent=self, pos=wx.Point(264, 72),
              size=wx.Size(80, 26), style=0, value=u'0')

        self.staticText7 = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONSTATICTEXT7,
              label=u'\u91d1\u535a\uff1a', name='staticText7', parent=self,
              pos=wx.Point(368, 80), size=wx.Size(51, 16), style=0)

        self.staticText8 = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONSTATICTEXT8,
              label=u'\u6b66\u9e23\uff1a', name='staticText8', parent=self,
              pos=wx.Point(544, 80), size=wx.Size(51, 16), style=0)

        self.staticText9 = wx.StaticText(id=wxID_PANELPRODUCTVARIATIONSTATICTEXT9,
              label=u'\u8f66\u7ba1\u6240\uff1a', name='staticText9',
              parent=self, pos=wx.Point(728, 80), size=wx.Size(68, 16),
              style=0)

        self.tcJinbo = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCJINBO,
              name=u'tcJinbo', parent=self, pos=wx.Point(424, 72),
              size=wx.Size(40, 26), style=0, value=u'')
        #self.tcJinbo.SetEditable(False)

        self.tcWuming = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCWUMING,
              name=u'tcWuming', parent=self, pos=wx.Point(608, 72),
              size=wx.Size(40, 26), style=0, value=u'')
        #self.tcWuming.SetEditable(False)

        self.tcCheguansuo = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCCHEGUANSUO,
              name=u'tcCheguansuo', parent=self, pos=wx.Point(792, 72),
              size=wx.Size(40, 26), style=0, value=u'')
        #self.tcCheguansuo.SetEditable(False)

        self.tc_JinboPos = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTC_JINBOPOS,
              name=u'tc_JinboPos', parent=self, pos=wx.Point(472, 72),
              size=wx.Size(56, 26), style=0, value=u'')
        #self.tc_JinboPos.SetEditable(False)

        self.tcWumingPos = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCWUMINGPOS,
              name=u'tcWumingPos', parent=self, pos=wx.Point(656, 72),
              size=wx.Size(56, 26), style=0, value=u'')
        #self.tcWumingPos.SetEditable(False)

        self.tcCheguansuoPos = wx.TextCtrl(id=wxID_PANELPRODUCTVARIATIONTCCHEGUANSUOPOS,
              name=u'tcCheguansuoPos', parent=self, pos=wx.Point(840, 72),
              size=wx.Size(48, 26), style=0, value=u'')
        #self.tcCheguansuoPos.SetEditable(False)

        self.checkBox_showPeiHuo = wx.CheckBox(id=wxID_PANELPRODUCTVARIATIONCHECKBOX_SHOWPEIHUO,
              label=u'\u663e\u793a\u914d\u8d27\u5355',
              name=u'checkBox_showPeiHuo', parent=self, pos=wx.Point(928, 72),
              size=wx.Size(128, 16), style=0)
        self.checkBox_showPeiHuo.SetValue(False)
        self.checkBox_showPeiHuo.Bind(wx.EVT_CHECKBOX,
              self.OnCheckBox_showPeiHuoCheckbox,
              id=wxID_PANELPRODUCTVARIATIONCHECKBOX_SHOWPEIHUO)


    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.lcPurDetail = MyListCtrlProPurDetail(self)
        self.lcPurDetail.SetSize(wx.Size(530, 550))
        self.lcPurDetail.Move(wx.Point(10, 100))
        
        self.lcSaleDetail = MyListCtrlProSaleDetail(self)
        self.lcSaleDetail.SetSize(wx.Size(530, 550))
        self.lcSaleDetail.Move(wx.Point(560, 100))
        
        self.lcCustDetail = MyListCtrlProSaleDetail(self)
        self.lcCustDetail.SetSize(wx.Size(530, 240))
        self.lcCustDetail.Move(wx.Point(560, 410))
        self.lcCustDetail.Hide()
        
        self.m_peerId = None
        self.checkBox_region.Hide()

        False
        
    def setData(self, proId, peerId=None):
        proObj = self.m_pro = CDataProduct.findObj(proId)
        
        self.tcName.SetValue(proObj.getValue(proObj.COL_NAME))
        self.tcSpec.SetValue(proObj.getValue(proObj.COL_SPEC))
        self.TCPosition.SetValue(proObj.getValue(proObj.COL_POSITION))
        self.TCMemo.SetValue(proObj.getValue(proObj.COL_MEMO))
        self.tc_weight.SetValue(str(proObj.getValue(proObj.COL_WEIGHT)))
        self.tc_freight_charges.SetValue(str(proObj.getValue(proObj.COL_FREIGHT)))
        
        self.m_pro.refreshOnhand()
        self.resetOnhand()
        self.resetManualNum()
        
        self.lcPurDetail.refresh({'proId':proId})
        self.lcSaleDetail.refresh({'proId':proId}) 
        
        self.m_peerId = peerId
        if (peerId is not None):
            self.checkBox_region.Show()
            self.lcCustDetail.Show()
            self.lcSaleDetail.SetSize(wx.Size(530, 300))
            self.lcCustDetail.refresh({'peerId':peerId, 'proId':proId}) 
            
        self.m_pro = proObj
        
        self.EnableEdit(False)

    def resetOnhand(self):
        proObj = self.m_pro
        onhand = u'%g' %(proObj.getValue(proObj.COL_ONHAND1))
        self.tcOnhand.SetValue(onhand)

        quantity_1,pos_1,quantity_2,pos_2,quantity_3,pos_3 = CDataProduct.GetStockQty(self.m_pro.getKeyVal())
      #   self.tcJinbo.SetValue(str(quantity_1)+","+pos_1)
      #   self.tcWuming.SetValue(str(quantity_2)+","+pos_2)
      #   self.tcCheguansuo.SetValue(str(quantity_3)+","+pos_3)
        self.tcJinbo.SetValue('%g' %(quantity_1 or 0))
        self.tcWuming.SetValue('%g' %(quantity_2 or 0))
        self.tcCheguansuo.SetValue('%g' %(quantity_3 or 0))
        self.tc_JinboPos.SetValue(pos_1)
        self.tcWumingPos.SetValue(pos_2)
        self.tcCheguansuoPos.SetValue(pos_3)

        
    def resetManualNum(self):
        proObj = self.m_pro
        manualNum = proObj.getValue(proObj.COL_MANUALNUM)
        if manualNum:
            self.tcManualNum.SetValue(manualNum)
        
    def OnBtnModifyOnhandButton(self, event):
        onhand = self.m_pro.getValue(self.m_pro.COL_ONHAND1)
        
        dlg = wx.TextEntryDialog(self, u'修改库存：', u'修改库存：', str(onhand))
        if (dlg.ShowModal() == wx.ID_OK):
            #self.m_pro.setValue(self.m_pro.COL_ONHAND1, float(dlg.GetValue()))
            #self.m_pro.update()
            self.m_pro.updateQuantity(float(dlg.GetValue()))
            
        self.resetOnhand()

    def OnBtnManualNumButton(self, event):
        manualNum = self.m_pro.getValue(self.m_pro.COL_MANUALNUM)
        if not manualNum:
            manualNum = ''
            
        dlg = wx.TextEntryDialog(self, u'修改手工编号：', u'修改手工编号：', manualNum)
        if (dlg.ShowModal() == wx.ID_OK):
            self.m_pro.setValue(self.m_pro.COL_MANUALNUM, dlg.GetValue())
            self.m_pro.update()
            
        self.resetManualNum()


    def OnBtn_saveButton(self, event):
        
        self.m_pro.setValue(self.m_pro.COL_NAME, self.tcName.GetValue())
        self.m_pro.setValue(self.m_pro.COL_SPEC, self.tcSpec.GetValue())
        self.m_pro.setValue(self.m_pro.COL_POSITION, self.TCPosition.GetValue())
        self.m_pro.setValue(self.m_pro.COL_MEMO, self.TCMemo.GetValue())
        self.m_pro.setValue(self.m_pro.COL_WEIGHT, self.tc_weight.GetValue())
        self.m_pro.setValue(self.m_pro.COL_FREIGHT, self.tc_freight_charges.GetValue())
        self.m_pro.update()
        
        # 保存库存
        CDataProduct.SetStockInfo(self.m_pro.getKeyVal()
            , float(self.tcJinbo.GetValue()) if self.tcJinbo.GetValue() != "" else None
            , self.tc_JinboPos.GetValue() if self.tc_JinboPos.GetValue() != "" else None
            , float(self.tcWuming.GetValue()) if self.tcWuming.GetValue() != "" else None
            , self.tcWumingPos.GetValue() if self.tcWumingPos.GetValue() != "" else None
            , float(self.tcCheguansuo.GetValue()) if self.tcCheguansuo.GetValue() != "" else None
            , self.tcCheguansuoPos.GetValue() if self.tcCheguansuoPos.GetValue() != "" else None)

        #self.m_pro.updateQuantity(float(self.tcOnhand.GetValue()))
        self.resetOnhand()
        

        # 更新缓存
        obj = CDataProduct.getInstance({'id':self.m_pro.getKeyVal()})
        obj.setValue(obj.COL_NAME, self.tcName.GetValue())
        obj.setValue(obj.COL_SPEC, self.tcSpec.GetValue())
        #obj.setValue(obj.COL_POSITION, self.TCPosition.GetValue())
        obj.setValue(obj.COL_MEMO, self.TCMemo.GetValue())
        obj.setValue(obj.COL_WEIGHT, float(self.tc_weight.GetValue()))
        obj.setValue(obj.COL_FREIGHT, float(self.tc_freight_charges.GetValue()))
        
        self.EnableEdit(False)

    def EnableEdit(self, bEnable=True):
        self.tcName.Enable(bEnable)
        self.tcSpec.Enable(bEnable)
        #self.TCPosition.Enable(bEnable)
        self.TCMemo.Enable(bEnable)
        self.tc_weight.Enable(bEnable)
        self.tc_freight_charges.Enable(bEnable)
        self.btn_save.Enable(bEnable)

        self.tcJinbo.Enable(bEnable)
        self.tc_JinboPos.Enable(bEnable)
        self.tcWuming.Enable(bEnable)
        self.tcWumingPos.Enable(bEnable)
        self.tcCheguansuo.Enable(bEnable)
        self.tcCheguansuoPos.Enable(bEnable)
        
    def OnButton_enableButton(self, event):
        self.EnableEdit()
        # self.tcName.Enable(True)
        # self.tcSpec.Enable(True)
        # #self.TCPosition.Enable(True)
        # self.TCMemo.Enable(True)
        # self.btn_save.Enable(True)
        # self.tc_weight.Enable(True)
        # self.tc_freight_charges.Enable(True)

    def OnCheckBox_regionCheckbox(self, event):
        
##        if (self.checkBox_region.GetValue()):
##            peerObj = CDataCustomer.findObj(self.m_peerId)
##            self.lcSaleDetail.refresh({'proId':self.m_pro.getKeyVal(), 
##                "regionId":peerObj.getValue(peerObj.COL_REGION)})
##        else:
##            self.lcSaleDetail.refresh({'proId':self.m_pro.getKeyVal()})
        self.refreshSaleDetail()

    def OnCheckBox_showPeiHuoCheckbox(self, event):
        self.refreshSaleDetail()
        
    def refreshSaleDetail(self):
        
        filter = {'proId':self.m_pro.getKeyVal()}
        if (self.checkBox_region.GetValue()):
            peerObj = CDataCustomer.findObj(self.m_peerId)
            filter["regionId"] = peerObj.getValue(peerObj.COL_REGION)
            
        if (self.checkBox_showPeiHuo.GetValue()):
            filter["showPeiHuo"] = 1
        self.lcSaleDetail.refresh(filter)
        
